package cn.demo.Puzzle;

import java.awt.Rectangle;

import javax.swing.*;

/**
 * 用于封装一个单元图片对象
 *
 * @author HP
 */

public class Cell extends JButton {

    private static final long serialVersionUID = 8290188097137862984L;
    public static final int IMAGEWIDTH = 200; // 图片宽度
    private int place;// 图片位置

    public Cell(ImageIcon icon, int place) {
        this.setIcon(icon); // 单元图片的图标
        this.setSize(IMAGEWIDTH, IMAGEWIDTH);// 单元图片的大小
        this.place = place; // 单元图片的位置
    }

    public void setStateIcon(ImageIcon icon) {
        this.setIcon(icon);
    }

    /*
     * 移动单元图片的方法
     */
    public void move(Direction dir) {
        Rectangle rec = this.getBounds();
        switch (dir) {
            case UP:
                this.setLocation(rec.x, rec.y - IMAGEWIDTH);
                break;
            case DOWN:
                this.setLocation(rec.x, rec.y + IMAGEWIDTH);
                break;
            case LEFT:
                this.setLocation(rec.x - IMAGEWIDTH, rec.y);
                break;
            case RIGHT:
                this.setLocation(rec.x + IMAGEWIDTH, rec.y);
                break;
        }
    }

    @Override
    public int getX() {
        return this.getBounds().x;
    }

    @Override
    public int getY() {
        return this.getBounds().y;
    }

    public int getPlace() {
        return place;
    }
}
